﻿#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip> 

namespace sy
{
	struct task
	{
		std::int32_t taskID;
		std::int32_t D;
		std::int32_t W;
	};
}

using sytask = sy::task;


namespace sy
{
	void PrintTask(int offset, int gap, const std::vector<sytask>& TSK,std::string tableName);
}

int main()
{
	std::ifstream fin("lab-4-in.txt");			// 数据读入
	std::ofstream fout("lab-4-out.txt");			// 数据输出
	if (!fin.is_open() || !fout.is_open()) {
		return -1;		// 文件打开失败
	}

	int numsOftask;		// 任务数量
	fin >> numsOftask;
	std::vector<sytask> TSK(numsOftask);	// 处罚
	std::vector<int>	record(numsOftask, 0);
	std::vector<sytask> inTimeTask;
	std::vector<sytask> overTimeTask;



	/*初始化处罚与截止时间*/
	std::for_each(TSK.begin(), TSK.end(), [&fin](sytask& data) {
		fin >> data.D;
	});
	std::for_each(TSK.begin(), TSK.end(), [&fin](sytask& data) {
		fin >> data.W;
	});
	// 分配任务 ID number
	int taskNum = 0;
	std::for_each(TSK.begin(), TSK.end(), [&taskNum](sytask& data) {
		data.taskID = ++taskNum;
	});

	/*以W为关键字进行降序排序*/
	std::sort(TSK.begin(), TSK.end(), [](const sytask& a, const sytask& b) {
		return a.W > b.W;
	});

	// 求解处理
	for (int i = 0, j; i < TSK.size(); i++) {
		for (j = TSK[i].D - 1; j < TSK.size(); j++) {
			if (record[j] > j )			// N(A) >= t
				break;
		}
		if (j == TSK.size()) {			// N(A) < t
			for (j = TSK[i].D - 1; j < TSK.size(); j++) {
				record[j]++;
			}
			inTimeTask.push_back(TSK[i]);
		}
		else{
			overTimeTask.push_back(TSK[i]);
		}
	}


	// 下面是显示结果的代码
	fout << "被选中任务 ：" << std::endl;
	std::for_each(inTimeTask.begin(), inTimeTask.end(), [&fout](sytask& tsk) {
		fout << tsk.taskID << "号任务" << std::endl;
		fout << "截止时间：" << tsk.D << std::endl;
		fout << "惩    罚：" << tsk.W << std::endl;
	});

	fout << "\n未被选中任务 ：" << std::endl;
	std::for_each(overTimeTask.begin(), overTimeTask.end(), [&fout](sytask& tsk) {
		fout << tsk.taskID << "号任务" << std::endl;
		fout << "截止时间：" << tsk.D << std::endl;
		fout << "惩    罚：" << tsk.W << std::endl;
	});
	fout << "\n总惩罚 ：" << std::endl;
	int totalW = 0;
	std::for_each(overTimeTask.begin(), overTimeTask.end(), [&fout,&totalW](sytask& tsk) {
		totalW += tsk.W;
	});



	std::sort(TSK.begin(), TSK.end(), [](const sytask& a, const sytask& b) {
		return a.taskID < b.taskID;
	});
	sy::PrintTask(27, 20, TSK, "Total tasks");
	std::sort(inTimeTask.begin(), inTimeTask.end(), [](const sytask& a, const sytask& b) {
		return a.D < b.D;
	});
	sy::PrintTask(17, 20, inTimeTask,"Selected tasks");
	sy::PrintTask(5,  20, overTimeTask, "Unselected tasks");

//------------------------ total W -------------------------
	//--------------------
	for (int i = 0; i < 20; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < 2; i++)  std::cout << "-------+";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < 20; i++) std::cout << " ";
	std::cout << "|total W|" << std::setw(7) << totalW << "|";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < 20; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < 2; i++)  std::cout << "-------+";
	std::cout << "\n";
	//--------------------

	return 0;
}

void sy::PrintTask(int offset, int gap, const std::vector<sytask> &TSK, std::string tableName)
{
	offset += gap;
	//--------------------
	for (int i = 0; i < offset; i++) {
		std::cout << " ";
	}
	std::cout << tableName << std::endl;
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < TSK.size() + 1; i++)  std::cout << "-------+";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "|task ID|";
	for (int i = 0; i < TSK.size(); i++)  std::cout << std::setw(7) << TSK[i].taskID << "|";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < TSK.size() + 1; i++)  std::cout << "-------+";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "|   D   |";
	for (int i = 0; i < TSK.size(); i++)  std::cout << std::setw(7) << TSK[i].D << "|";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < TSK.size() + 1; i++)  std::cout << "-------+";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "|   W   |";
	for (int i = 0; i < TSK.size(); i++)  std::cout << std::setw(7) << TSK[i].W << "|";
	std::cout << "\n";
	//--------------------
	for (int i = 0; i < gap; i++) std::cout << " ";
	std::cout << "+";
	for (int i = 0; i < TSK.size() + 1; i++)  std::cout << "-------+";
	std::cout << "\n";
	std::cout << "\n\n";
}